package com.neu.his.controller.SystemManage01.RtManage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.neu.his.core.Result;
import com.neu.his.entity.Regist;
import com.neu.his.entity.RegistLevel;
import com.neu.his.entity.VO.SystemManage01.RtManage.RtVO;
import com.neu.his.entity.request.DeleteParams;
import com.neu.his.entity.request.PageParams;
import com.neu.his.entity.request.registType.RtParams;
import com.neu.his.entity.request.registType.RtUpdateParams;
import com.neu.his.mapper.RegistLevelMapper;
import com.neu.his.service.RtService;
import com.neu.his.utils.PageList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: neu-his
 * <p>
 * Created by 31316 on 2020/7/18 17:09
 */
@RestController
@RequestMapping("/registration_type")
public class RtManageController {
    @Autowired
    RtService rtService;

    @Autowired
    RegistLevelMapper registLevelMapper;

    @PostMapping
    public Result insertOne(@RequestBody RtParams rtParams) {
        if (registLevelMapper.selectOne(new QueryWrapper<RegistLevel>().ne("remove_mark", 1L)
                .eq("regist_level", rtParams.getRegistLevel())) != null)
            return Result.fail("已存在");
        RegistLevel registLevel = new RegistLevel();
        registLevel.setRegistLevel(rtParams.getRegistLevel());
        registLevel.setRegistCode(rtParams.getRegistCode());
        registLevel.setRegistCost(rtParams.getRegistCost());
        registLevel.setRegistDefalut(rtParams.getRegistDefalut());
        registLevel.setRegistOrder(rtParams.getRegistOrder());
        registLevel.setCreateDate();
        registLevelMapper.insert(registLevel);
        return Result.success("插入成功");
    }

    @PostMapping("/query")
    public Result query(@RequestBody PageParams pageParams) {
        PageList pageList =new PageList(rtService.selectAllList(),pageParams.getCurrentPage(),pageParams.getPageSize());
        return Result.success(pageList);
    }

    @PutMapping
    public Result updateOne(@RequestBody RtUpdateParams rtUpdateParams){
        RegistLevel registLevel;
        if((registLevel=registLevelMapper.selectOne(new QueryWrapper<RegistLevel>().eq("id",rtUpdateParams.getId())
                .ne("remove_mark",1L)))==null)
            return Result.fail("不存在");
        registLevel.setUpdateDate();
        if(StringUtils.isNotEmpty(rtUpdateParams.getRegistCode())) registLevel.setRegistCode(rtUpdateParams.getRegistCode());
        if(StringUtils.isNotEmpty(rtUpdateParams.getRegistDefalut())) registLevel.setRegistDefalut(rtUpdateParams.getRegistDefalut());
        if(StringUtils.isNotEmpty(rtUpdateParams.getRegistLevel())) registLevel.setRegistLevel(rtUpdateParams.getRegistLevel());
        if(rtUpdateParams.getRegistOrder()!=0) registLevel.setRegistOrder(rtUpdateParams.getRegistOrder());
        if(rtUpdateParams.getRegistCost()!=null) registLevel.setRegistCost(rtUpdateParams.getRegistCost());
        registLevelMapper.updateById(registLevel);
        return Result.success("修改成功");
    }

    @DeleteMapping
    public Result delete(@RequestBody DeleteParams deleteParams){
        UpdateWrapper<RegistLevel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", deleteParams.getIds());
        updateWrapper.set("remove_mark", 1);
        registLevelMapper.update(null, updateWrapper);
        return Result.success(new ArrayList<Long>());
    }

    @GetMapping("/all")
    public Result getAllRt(){
        return Result.success(rtService.selectAllList());
    }
}
